<?php
namespace App\Traits;

use App\Services\Api\Api;

trait CaseTrait
{
    public function getCaseList($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'name' => $data['case_name'],
            'authStatus' => $data['status'],
            'pageNo' => $data['page_number'],
            'pageSize' => $data['page_limit'],
            'custId' => $data['company_id'],
            'province' => $data['case_address_id'],
            'domain' => -1,
        ];

        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result) {
            return [
                'dataList' => $result['cases'],
                'pageCount' => $result['pageTotal'],
            ];
        }
        return false;
    }

    public function getCompanyCaseInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'caseId' => $data['id'],
            'custId' => $data['company_id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return [
                'case_name' => $result['name'],
                'case_address_id' => $result['province'],
                'case_domain_id' => trim($result['domain'], ','),
                'case_image' => $result['pic'],
                'case_content' => $result['intro'],
                'tech_id' => $result['techId'],
                'case_type' => $result['type'],
            ];
        }
        return false;
    }

    public function addCompanyCaseInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'caseId' => -1,
            'custId' => $data['company_id'],
            'name' => $data['case_name'], //案例名称
            'province' => $data['case_address_id'], //省ID
            'domain' => ',' . $data['case_domain_id'] . ',', //逗号隔开
            'pic' => $data['case_image'], //案例图片
            'intro' => $data['case_content'], //案例介绍
            'techId' => $data['tech_id'] ? $data['tech_id'] : -1, //技术ID
            'type' => $data['type'], //案例类型
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return $result['caseId'];
        }
        return false;
    }

    public function editCompanyCaseInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'caseId' => $data['id'],
            'custId' => $data['company_id'],
            'name' => $data['case_name'], //案例名称
            'province' => $data['case_address_id'], //省ID
            'domain' => ',' . $data['case_domain_id'] . ',', //逗号隔开
            'pic' => $data['case_image'], //案例图片
            'intro' => $data['case_content'], //案例介绍
            'techId' => $data['tech_id'] ? $data['tech_id'] : -1, //技术ID
            'type' => $data['type'], //案例类型
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function checkCompanyCaseInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
            'caseId' => $data['id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }

    public function delCompanyCaseInfo($apiCode, $data)
    {
        $obj = new Api();
        $formData = [
            'custId' => $data['company_id'],
            'caseId' => $data['id'],
        ];
        $postData = $obj->getJavaPostData($apiCode, $formData);
        $result = $obj->getJavaResponseData($postData);
        if ($result && $result['succ'] == 1) {
            return true;
        }
        return false;
    }
}
